﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;
using Novacode;
using System.Configuration;

namespace ProcessData
{
    public class WordManager
    {
        private List<WordInfo> wordList;
        private String invitacionUri;
        private WordDateInfo wordDateInfo;

        private static readonly string FORMAT_DATE = ConfigurationManager.AppSettings["formatLetter"];
        private static readonly string FORMAT_HOUR_INITIAL = ConfigurationManager.AppSettings["formatHourInitial"];
        private static readonly string FORMAT_HOUR_FINAL = ConfigurationManager.AppSettings["formatHourFinal"];

        public WordManager(List<WordInfo> wl, String uri, WordDateInfo info)
        {
            wordList = wl;
            invitacionUri = uri;
            wordDateInfo = info;
        }

        private Dictionary<String, MemoryStream> ProcessTemplate()
        {
            MemoryStream plantillaStream = new MemoryStream();
            Dictionary<String, MemoryStream> listStream = new Dictionary<String, MemoryStream>();

            //Extraemos la plantilla
            using (DocX documento = DocX.Load(invitacionUri))
            {
                //Se modifican las fechas una sola vez en la plantilla
                documento.ReplaceText("[[Fecha Invitacion]]", wordDateInfo.FechaInvitacion.ToString(FORMAT_DATE));
                documento.ReplaceText("[[Fecha Mediacion]]", wordDateInfo.FechaMediacion.ToString(FORMAT_DATE));
                documento.ReplaceText("[[Hora Mediacion]]", String.Format("{0} a {1}", wordDateInfo.HoraInicio.ToString(FORMAT_HOUR_INITIAL), 
                    wordDateInfo.HoraFinal.ToString(FORMAT_HOUR_FINAL)));

                documento.SaveAs(plantillaStream);
            }

            foreach (WordInfo info in wordList)
            {
                MemoryStream memoryFile = new MemoryStream();

                using (DocX documento = DocX.Load(plantillaStream))
                {
                    documento.ReplaceText("[[NOMBRE]]", info.Nombre);
                    documento.ReplaceText("[[DIRECCION]]", info.DireccionCompleta);
                    documento.ReplaceText("[[CREDITO]]", info.Credito.ToString());

                    documento.SaveAs(memoryFile);
                }

                listStream.Add(info.Nombre, memoryFile);
            }

            return listStream;
        }

        public byte[] CreateResponse()
        {
            //Creamos la lista de stream a usar
            Dictionary<String,MemoryStream> listSream = ProcessTemplate();

            //Ahora creamos el archivo zip a partir del stream
            ZipFile zipFile = new ZipFile();
            foreach (KeyValuePair<String,MemoryStream> obj in listSream)
	        {
                ZipEntry entry = zipFile.AddEntry(obj.Key+".docx", obj.Value.ToArray());                
	        }

            MemoryStream memoryStream = new MemoryStream();
            zipFile.Save(memoryStream);

            return memoryStream.ToArray();
        }
    }
}
